package com.example.demo.mvvm.net;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by 80711 on 2018/5/16.
 */

public class RetrofitClient {

    private static RetrofitClient instance;

    private static final String BASE_URL = "http://www.wanandroid.com/";
    private Retrofit retrofit;

    private ApiService service ;

    private RetrofitClient(){
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(10 , TimeUnit.SECONDS)
                .readTimeout(10 , TimeUnit.SECONDS)
                .addInterceptor(new CookiesInterceptor())
                .build();
        retrofit = new Retrofit.Builder()
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())// 针对rxjava2.x
                .baseUrl(BASE_URL)
                .build();
        service = retrofit.create(ApiService.class);
    }


    public static synchronized RetrofitClient getInstance(){
        if(instance == null)
            instance = new RetrofitClient();
        return instance;
    }

    public ApiService getService() {
        return service;
    }
}
